package ru.kreda.webapp.client;

import ru.kreda.webapp.client.event.OpenHideSplitPanelEvent;
import ru.kreda.webapp.client.gin.AppGinjector;
import ru.kreda.webapp.client.logic.AppController;
import ru.kreda.webapp.client.mvp.AppPlaceHistoryMapper;
import ru.kreda.webapp.client.view.AppLayout;

import com.extjs.gxt.ui.client.GXT;
import com.extjs.gxt.ui.client.util.Theme;
import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Входная точка приложения. Здесь происходит установка нужной темы,
 * инициализация MVP и отображение главного экрана.
 */
public class AppEntryPoint implements EntryPoint {

	private AppGinjector injector = GWT.create(AppGinjector.class);

	@Override
	public void onModuleLoad() {
		GWT.setUncaughtExceptionHandler(new ErrorHandler());
        //Theme.BLUE.set("file", "resources/css/gxt-gray.css");
		GXT.setDefaultTheme(Theme.BLUE, true);

		AppLayout appLayoutView = new AppLayout();
		RootPanel.get().add(appLayoutView);

		EventBus eventBus = injector.getEventBus();
		PlaceController placeController = injector.getPlaceController();

		new AppController(eventBus, placeController, appLayoutView);
		eventBus.fireEvent(new OpenHideSplitPanelEvent(OpenHideSplitPanelEvent.KEY_REGISTER_LIST_PANEL, false, false));

		ActivityMapper contentActivityMapper = injector.getActivityMapper();
		ActivityManager activityManager = new ActivityManager(contentActivityMapper, eventBus);
		activityManager.setDisplay(appLayoutView.getContentPanel());

		AppPlaceHistoryMapper historyMapper = GWT.create(AppPlaceHistoryMapper.class);

		PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper);
		historyHandler.register(placeController, eventBus, Place.NOWHERE);

		historyHandler.handleCurrentHistory();
	}

}
